from django.core.urlresolvers import reverse_lazy
from django.shortcuts import resolve_url
from django.views.generic import ListView, CreateView, DeleteView
from scaffold_toolkit.views import AjaxView
from basedata.forms import SearchForm, FeatureForm, AreaForm
from basedata.models import City, Feature
from basedata.templatetags.my_template import city_tags
from ghouse import settings

__author__ = 'mayufeng'



class StateListView(ListView):
    model = City
    template_name = "basedata/state_list.html"
    paginate_by = settings.PAGE_SIZE

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(StateListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(StateListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {'category': 'S'}
        form = self.form
        form.is_valid()
        if form.cleaned_data['city_name']:
            kwargs['city_name__contains'] = form.cleaned_data['city_name']
        return self.model.objects.filter(**kwargs)



class StateCreate(CreateView):
    form_class = AreaForm
    model = City
    template_name = 'basedata/state_form.html'

    def get_context_data(self, **kwargs):
        context = super(StateCreate, self).get_context_data(**kwargs)
        context['category'] = 'S'
        return context

    def get_success_url(self):
        return resolve_url('state_list')



class StateDelete(DeleteView):
    model = City
    success_url = reverse_lazy('state_list')
    template_name = "accounts/confirm_delete.html"



class CityListView(ListView):
    model = City
    template_name = "basedata/city_list.html"
    paginate_by = settings.PAGE_SIZE

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(CityListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(CityListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {'category': 'C'}
        form = self.form
        form.is_valid()
        if form.cleaned_data['city_name']:
            kwargs['city_name__contains'] = form.cleaned_data['city_name']
        return self.model.objects.filter(**kwargs)



class CityCreate(CreateView):
    form_class = AreaForm
    model = City
    template_name = 'basedata/city_form.html'

    def get_context_data(self, **kwargs):
        context = super(CityCreate, self).get_context_data(**kwargs)
        context['category'] = 'C'
        return context

    def get_success_url(self):
        return resolve_url('city_list')



class CityDelete(DeleteView):
    model = City
    success_url = reverse_lazy('city_list')
    template_name = "accounts/confirm_delete.html"



class AreaListView(ListView):
    model = City
    template_name = "basedata/area_list.html"
    paginate_by = settings.PAGE_SIZE

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(AreaListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(AreaListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {'category': 'A'}
        form = self.form
        form.is_valid()
        if form.cleaned_data['city_name']:
            kwargs['city_name__contains'] = form.cleaned_data['city_name']
        return self.model.objects.filter(**kwargs)


class AreaCreate(CreateView):
    form_class = AreaForm
    model = City
    template_name = 'basedata/area_form.html'

    def get_context_data(self, **kwargs):
        context = super(AreaCreate, self).get_context_data(**kwargs)
        context['category'] = 'A'
        return context

    def get_success_url(self):
        return resolve_url('area_list')



class AreaDelete(DeleteView):
    model = City
    success_url = reverse_lazy('area_list')
    template_name = "accounts/confirm_delete.html"



class FeatureListView(ListView):
    model = Feature
    template_name = "basedata/feature_list.html"
    paginate_by = settings.PAGE_SIZE

    def get(self, request, *args, **kwargs):
        self.form = SearchForm(request.GET)
        return super(FeatureListView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(FeatureListView, self).get_context_data(**kwargs)
        context['form'] = self.form
        return context

    def get_queryset(self):
        kwargs = {}
        form = self.form
        form.is_valid()
        if form.cleaned_data['name']:
            kwargs['name__contains'] = form.cleaned_data['name']
        return self.model.objects.filter(**kwargs)



class FeatureCreate(CreateView):
    form_class = FeatureForm
    model = Feature
    template_name = 'basedata/city_form.html'

    def get_success_url(self):
        return resolve_url('feature_list')



class FeatureDelete(DeleteView):
    model = Feature
    success_url = reverse_lazy('feature_list')
    template_name = "accounts/confirm_delete.html"



class CityAjaxView(AjaxView):
    def post_ajax(self, request, *args, **kwargs):
        type = request.POST["type"]
        parent_id = request.POST["parent_id"]
        select = city_tags(type, parent_id)
        return self.render_json_response({"success": True, "select": select})
